<!--
 * @Description: 进制转换算法
 * @Autor: wangxin
 * @Date: 2020-06-10 08:56:21
 * @LastEditors: Seven
 * @LastEditTime: 2020-06-10 08:56:48
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script src="./stack.js"></script>
    <script>
      function baseConverter(decimalNumber, base) {
        const remStack = new Stack()
        const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        let number = decimalNumber
        let rem
        let baseString = ''

        if (!(base >= 2 && base <= 36)) {
          return ''
        }

        while (number > 0) {
          rem = Math.floor(number % base)
          remStack.push(rem)
          number = Math.floor(number / base)
        }

        while (!remStack.isEmpty()) {
          baseString += digits[remStack.pop()]
        }

        return baseString
      }

      // 测试代码
      console.log(baseConverter(100345, 2))
      console.log(baseConverter(100345, 8))
      console.log(baseConverter(100345, 16))
    </script>
  </body>
</html>
